<template>
  <div class="ai-management">
    <div class="page-header">
      <h2>AI算法管理</h2>
      <p class="page-description">管理和监控AI算法模型，优化算法性能</p>
    </div>
    
    <!-- 概览卡片 -->
    <div class="overview-cards">
      <el-row :gutter="24">
        <el-col :xs="24" :sm="12" :md="6">
          <div class="overview-card">
            <div class="card-icon running">
              <el-icon><VideoPlay /></el-icon>
            </div>
            <div class="card-content">
              <div class="card-value" v-loading="overviewData.loading">
                {{ overviewData.runningAlgorithms }}/{{ overviewData.totalAlgorithms }}
              </div>
              <div class="card-label">运行中算法</div>
            </div>
          </div>
        </el-col>
        
        <el-col :xs="24" :sm="12" :md="6">
          <div class="overview-card">
            <div class="card-icon gpu">
              <el-icon><Cpu /></el-icon>
            </div>
            <div class="card-content">
              <div class="card-value" v-loading="overviewData.loading">{{ overviewData.gpuUsage }}%</div>
              <div class="card-label">GPU使用率</div>
            </div>
          </div>
        </el-col>
        
        <el-col :xs="24" :sm="12" :md="6">
          <div class="overview-card">
            <div class="card-icon performance">
              <el-icon><TrendCharts /></el-icon>
            </div>
            <div class="card-content">
              <div class="card-value" v-loading="overviewData.loading">{{ overviewData.avgAccuracy }}%</div>
              <div class="card-label">平均准确率</div>
            </div>
          </div>
        </el-col>
        
        <el-col :xs="24" :sm="12" :md="6">
          <div class="overview-card">
            <div class="card-icon alerts">
              <el-icon><Warning /></el-icon>
            </div>
            <div class="card-content">
              <div class="card-value" v-loading="overviewData.loading">{{ overviewData.alerts }}</div>
              <div class="card-label">告警数量</div>
            </div>
          </div>
        </el-col>
      </el-row>
    </div>
    
    <!-- 功能模块 -->
    <div class="management-modules">
      <div class="module-card">
        <el-tabs v-model="activeTab" type="border-card">
          <el-tab-pane label="算法管理" name="algorithms">
            <AlgorithmManagement />
          </el-tab-pane>
          
          <el-tab-pane label="摄像头绑定" name="camera-binding">
            <CameraAlgorithmBinding />
          </el-tab-pane>
          
          <el-tab-pane label="算法配置" name="config">
            <AlgorithmConfig />
          </el-tab-pane>
          
          <el-tab-pane label="GPU监控" name="gpu-monitor">
            <div class="gpu-monitor-section">
              <GPUMonitor />
            </div>
          </el-tab-pane>
        </el-tabs>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted, onUnmounted } from 'vue';
import { ElMessage } from 'element-plus';
import AlgorithmManagement from './components/AlgorithmManagement.vue';
import CameraAlgorithmBinding from './components/CameraAlgorithmBinding.vue';
import GPUMonitor from './components/GPUMonitor.vue';
import AlgorithmConfig from './components/AlgorithmConfig.vue';
import { getAIServicesOverview } from '@/api/ai-algorithm';
import { gpuApi } from '@/api/gpu';

// 响应式数据
const activeTab = ref('algorithms');
const overviewData = reactive({
  runningAlgorithms: 0,
  totalAlgorithms: 6,
  gpuUsage: 0,
  avgAccuracy: 0,
  alerts: 0,
  loading: false
});

let updateTimer: NodeJS.Timeout | null = null;

// 加载概览数据
const loadOverviewData = async () => {
  overviewData.loading = true;
  try {
    // 获取AI服务概览
    const aiResponse = await getAIServicesOverview();
    if (aiResponse.code === 200 && aiResponse.data) {
      const data = aiResponse.data;
      overviewData.runningAlgorithms = data.runningAlgorithms || 0;
      overviewData.totalAlgorithms = data.totalAlgorithms || 6;
      overviewData.avgAccuracy = data.avgAccuracy || 0;
      overviewData.alerts = data.alerts || 0;
    }

    // 获取GPU使用率
    try {
      const gpuResponse = await gpuApi.getStatus();
      if (gpuResponse.code === 200 && gpuResponse.data) {
        const gpuData = Array.isArray(gpuResponse.data) ? gpuResponse.data : [gpuResponse.data];
        if (gpuData.length > 0) {
          // 计算平均GPU使用率
          const totalUsage = gpuData.reduce((sum, gpu) => sum + (gpu.usage?.gpu || 0), 0);
          overviewData.gpuUsage = Math.round(totalUsage / gpuData.length);
        }
      }
    } catch (gpuError) {
      console.warn('获取GPU状态失败，使用模拟数据:', gpuError);
      overviewData.gpuUsage = Math.floor(Math.random() * 30) + 60;
    }

  } catch (error) {
    console.error('加载概览数据失败:', error);
    // 使用模拟数据作为后备
    overviewData.runningAlgorithms = Math.floor(Math.random() * 4) + 2;
    overviewData.gpuUsage = Math.floor(Math.random() * 30) + 60;
    overviewData.avgAccuracy = Math.floor(Math.random() * 10) + 90;
    overviewData.alerts = Math.floor(Math.random() * 5);
  } finally {
    overviewData.loading = false;
  }
};

// 实时数据更新
const updateOverviewData = () => {
  if (!overviewData.loading) {
    loadOverviewData();
  }
};

onMounted(() => {
  console.log('AI算法管理页面初始化');
  
  // 初始加载数据
  loadOverviewData();
  
  // 每30秒更新一次数据
  updateTimer = setInterval(updateOverviewData, 30000);
});

onUnmounted(() => {
  if (updateTimer) {
    clearInterval(updateTimer);
  }
});
</script>

<style lang="scss" scoped>
.ai-management {
  padding: 24px;
  min-height: 100%;
  background: #f5f7fa;
  
  .page-header {
    margin-bottom: 24px;
    
    h2 {
      margin: 0 0 8px 0;
      font-size: 24px;
      font-weight: 600;
      color: #303133;
    }
    
    .page-description {
      margin: 0;
      color: #606266;
      font-size: 14px;
    }
  }
  
  .overview-cards {
    margin-bottom: 24px;
    
    .overview-card {
      display: flex;
      align-items: center;
      padding: 20px;
      background: white;
      border-radius: 12px;
      box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.08);
      border: 1px solid rgba(0, 0, 0, 0.06);
      transition: all 0.3s ease;
      position: relative;
      overflow: hidden;
      
      &::before {
        content: '';
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
        height: 3px;
        background: linear-gradient(90deg, transparent, rgba(64, 158, 255, 0.3), transparent);
        opacity: 0;
        transition: opacity 0.3s ease;
      }
      
      &:hover {
        transform: translateY(-4px);
        box-shadow: 0 8px 25px 0 rgba(0, 0, 0, 0.15);
        
        &::before {
          opacity: 1;
        }
      }
      
      .card-icon {
        width: 48px;
        height: 48px;
        border-radius: 8px;
        display: flex;
        align-items: center;
        justify-content: center;
        margin-right: 16px;
        
        .el-icon {
          font-size: 24px;
          color: white;
        }
        
        &.running {
          background: linear-gradient(135deg, #67c23a, #85ce61);
        }
        
        &.gpu {
          background: linear-gradient(135deg, #409eff, #66b1ff);
        }
        
        &.performance {
          background: linear-gradient(135deg, #e6a23c, #ebb563);
        }
        
        &.alerts {
          background: linear-gradient(135deg, #f56c6c, #f78989);
        }
      }
      
      .card-content {
        flex: 1;
        
        .card-value {
          font-size: 24px;
          font-weight: 600;
          color: #303133;
          line-height: 1;
          margin-bottom: 4px;
        }
        
        .card-label {
          font-size: 14px;
          color: #606266;
        }
      }
    }
  }
  
  .management-modules {
    .module-card {
      background: white;
      border-radius: 8px;
      box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
      overflow: hidden;
      
      .module-header {
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding: 20px 24px;
        border-bottom: 1px solid #e4e7ed;
        
        h3 {
          margin: 0;
          font-size: 16px;
          font-weight: 600;
          color: #303133;
        }
      }
      
      .gpu-monitor-section {
        padding: 20px;
        
        h4 {
          margin: 0 0 16px 0;
          font-size: 14px;
          font-weight: 600;
          color: #303133;
        }
      }
    }
  }
}

.dark .ai-management {
  background: #1a1a1a;
  
  .page-header {
    h2 {
      color: #e4e7ed;
    }
    
    .page-description {
      color: #909399;
    }
  }
  
  .overview-cards {
    .overview-card {
      background: #2d2d2d;
      
      .card-content {
        .card-value {
          color: #e4e7ed;
        }
        
        .card-label {
          color: #909399;
        }
      }
    }
  }
  
  .management-modules {
    .module-card {
      background: #2d2d2d;
      
      .module-header {
        border-bottom-color: #4c4d4f;
        
        h3 {
          color: #e4e7ed;
        }
      }
    }
  }
}

@media (max-width: 768px) {
  .ai-management {
    padding: 16px;
    
    .page-header {
      margin-bottom: 16px;
      
      h2 {
        font-size: 20px;
      }
    }
    
    .overview-cards {
      margin-bottom: 16px;
      
      .overview-card {
        padding: 16px;
        margin-bottom: 16px;
        
        .card-icon {
          width: 40px;
          height: 40px;
          margin-right: 12px;
          
          .el-icon {
            font-size: 20px;
          }
        }
        
        .card-content {
          .card-value {
            font-size: 20px;
          }
        }
      }
    }
    
    .management-modules {
      .module-card {
        margin-bottom: 16px;
        
        .module-header {
          padding: 16px;
          
          h3 {
            font-size: 14px;
          }
        }
      }
    }
  }
}
</style>